/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FileCatalogClientUI.java
 *
 * Created on Nov 23, 2011, 1:26:31 PM
 */
package ui;

import client.CatalogClient;
import client.CatalogClientImp;
import java.awt.Component;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import model.AccessPermission;
import model.CatalogFile;
import model.WriteReadPermission;

/**
 *
 * @author julio
 */
public class CatalogClientUI extends javax.swing.JFrame implements CatalogResponsiveUI {
    private CatalogFileTableModel allFilesModel;
    private CatalogFileTableModel myFilesModel;
    private CatalogClient client;

    /** Creates new form FileCatalogClientUI */
    public CatalogClientUI(String[] args) {
        initComponents();
        client = new CatalogClientImp(this, args);

        allFilesModel = new CatalogFileTableModel(new ArrayList<CatalogFile>());
        allFilesTable.setModel(allFilesModel);

        myFilesModel = new CatalogFileTableModel(new ArrayList<CatalogFile>());
        myFilesTable.setModel(myFilesModel);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jTabbedPane2 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        allFilesTable = new javax.swing.JTable();
        jPanel4 = new javax.swing.JPanel();
        allRefreshBtn = new javax.swing.JButton();
        allDownloadBtn = new javax.swing.JButton();
        allUpdateBtn = new javax.swing.JButton();
        allDeleteBtn = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        myFilesTable = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        myRefreshBtn = new javax.swing.JButton();
        myUploadBtn = new javax.swing.JButton();
        myDeleteBtn = new javax.swing.JButton();
        myUpdateBtn = new javax.swing.JButton();
        myDownloadBtn = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        loginMenuItem = new javax.swing.JMenuItem();
        logoutMenuItem = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        registerMenuItem = new javax.swing.JMenuItem();
        unregisterMenuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("File Catalog");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jPanel1.setPreferredSize(new java.awt.Dimension(559, 300));
        jPanel1.setLayout(new java.awt.GridBagLayout());

        jScrollPane1.setPreferredSize(new java.awt.Dimension(452, 300));

        allFilesTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(allFilesTable);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        jPanel1.add(jScrollPane1, gridBagConstraints);

        allRefreshBtn.setText("Refresh");
        allRefreshBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                allRefreshBtnActionPerformed(evt);
            }
        });

        allDownloadBtn.setText("Download");
        allDownloadBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                allDownloadBtnActionPerformed(evt);
            }
        });

        allUpdateBtn.setText("Update");
        allUpdateBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                allUpdateBtnActionPerformed(evt);
            }
        });

        allDeleteBtn.setText("Delete");
        allDeleteBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                allDeleteBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(allRefreshBtn, javax.swing.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE)
                    .addComponent(allDeleteBtn, javax.swing.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE)
                    .addComponent(allUpdateBtn, javax.swing.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE)
                    .addComponent(allDownloadBtn, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(allRefreshBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 220, Short.MAX_VALUE)
                .addComponent(allDownloadBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(allUpdateBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(allDeleteBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        jPanel1.add(jPanel4, gridBagConstraints);

        jTabbedPane2.addTab("All Files", jPanel1);

        jPanel2.setLayout(new java.awt.GridBagLayout());

        myFilesTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane2.setViewportView(myFilesTable);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        jPanel2.add(jScrollPane2, gridBagConstraints);

        myRefreshBtn.setText("Refresh");
        myRefreshBtn.setMaximumSize(new java.awt.Dimension(79, 23));
        myRefreshBtn.setMinimumSize(new java.awt.Dimension(79, 23));
        myRefreshBtn.setPreferredSize(new java.awt.Dimension(79, 23));
        myRefreshBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                myRefreshBtnActionPerformed(evt);
            }
        });

        myUploadBtn.setText("Upload");
        myUploadBtn.setMaximumSize(new java.awt.Dimension(79, 23));
        myUploadBtn.setMinimumSize(new java.awt.Dimension(79, 23));
        myUploadBtn.setPreferredSize(new java.awt.Dimension(79, 23));
        myUploadBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                myUploadBtnActionPerformed(evt);
            }
        });

        myDeleteBtn.setText("Delete");
        myDeleteBtn.setMaximumSize(new java.awt.Dimension(79, 23));
        myDeleteBtn.setMinimumSize(new java.awt.Dimension(79, 23));
        myDeleteBtn.setPreferredSize(new java.awt.Dimension(79, 23));
        myDeleteBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                myDeleteBtnActionPerformed(evt);
            }
        });

        myUpdateBtn.setText("Update");
        myUpdateBtn.setMaximumSize(new java.awt.Dimension(79, 23));
        myUpdateBtn.setMinimumSize(new java.awt.Dimension(79, 23));
        myUpdateBtn.setPreferredSize(new java.awt.Dimension(79, 23));
        myUpdateBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                myUpdateBtnActionPerformed(evt);
            }
        });

        myDownloadBtn.setText("Download");
        myDownloadBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                myDownloadBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(myUpdateBtn, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(myRefreshBtn, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(myUploadBtn, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(myDownloadBtn, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(myDeleteBtn, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(myRefreshBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(myUploadBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 189, Short.MAX_VALUE)
                .addComponent(myDownloadBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(myUpdateBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(myDeleteBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        jPanel2.add(jPanel3, gridBagConstraints);

        jTabbedPane2.addTab("My Files", jPanel2);

        getContentPane().add(jTabbedPane2, java.awt.BorderLayout.CENTER);

        jMenu1.setText("Catalog");

        loginMenuItem.setText("Login...");
        loginMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginMenuItemActionPerformed(evt);
            }
        });
        jMenu1.add(loginMenuItem);

        logoutMenuItem.setText("Logout");
        logoutMenuItem.setEnabled(false);
        logoutMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                logoutMenuItemActionPerformed(evt);
            }
        });
        jMenu1.add(logoutMenuItem);
        jMenu1.add(jSeparator1);

        registerMenuItem.setText("Register...");
        registerMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                registerMenuItemActionPerformed(evt);
            }
        });
        jMenu1.add(registerMenuItem);

        unregisterMenuItem.setEnabled(false);
        unregisterMenuItem.setLabel("Unregister");
        unregisterMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                unregisterMenuItemActionPerformed(evt);
            }
        });
        jMenu1.add(unregisterMenuItem);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-767)/2, (screenSize.height-441)/2, 767, 441);
    }// </editor-fold>//GEN-END:initComponents

private void loginMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginMenuItemActionPerformed
    (new LoginDialog(this, true)).setVisible(true);
}//GEN-LAST:event_loginMenuItemActionPerformed

private void myUploadBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_myUploadBtnActionPerformed
    (new UploadFileDialog(this, true)).setVisible(true);
}//GEN-LAST:event_myUploadBtnActionPerformed

private void myDownloadBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_myDownloadBtnActionPerformed
    final CatalogFile catalogDesc = myFilesModel.getCatalogFile(myFilesTable.getSelectedRow());

    Runnable downloadTask = new Runnable() {
        @Override
        public void run() {
            client.downloadFile(catalogDesc);
        }
    };
    (new Thread(downloadTask)).start();
}//GEN-LAST:event_myDownloadBtnActionPerformed

private void registerMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_registerMenuItemActionPerformed
    (new RegisterDialog(this, true)).setVisible(true);
}//GEN-LAST:event_registerMenuItemActionPerformed

private void myUpdateBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_myUpdateBtnActionPerformed
    CatalogFile fileToUpdate = myFilesModel.getCatalogFile(myFilesTable.getSelectedRow());

    if (fileToUpdate != null) {
        JFileChooser browseDlg = new JFileChooser();
        int result = browseDlg.showOpenDialog((Component) this);

        if (result == JFileChooser.APPROVE_OPTION) {
            String filePath = browseDlg.getSelectedFile().getPath();
            File file = new File(filePath);
            if (file.exists()) {
                updateFile(file, fileToUpdate);
            }
        }
    }
}//GEN-LAST:event_myUpdateBtnActionPerformed

private void myDeleteBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_myDeleteBtnActionPerformed
    final CatalogFile selectedFile = myFilesModel.getCatalogFile(myFilesTable.getSelectedRow());

    Runnable deleteTask = new Runnable() {
        @Override
        public void run() {
            client.deleteFile(selectedFile);
        }
    };
    (new Thread(deleteTask)).start();

}//GEN-LAST:event_myDeleteBtnActionPerformed

private void logoutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_logoutMenuItemActionPerformed
    Runnable logoutTask = new Runnable() {
        @Override
        public void run() {
            client.logout();
        }
    };

    (new Thread(logoutTask)).start();
}//GEN-LAST:event_logoutMenuItemActionPerformed

    private void allRefreshBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_allRefreshBtnActionPerformed
        Runnable getFilesTask = new Runnable() {
            @Override
            public void run() {
                client.getAllFiles();
            }
        };
        (new Thread(getFilesTask)).start();
    }//GEN-LAST:event_allRefreshBtnActionPerformed

    private void allDownloadBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_allDownloadBtnActionPerformed
        final CatalogFile catalogDesc = allFilesModel.getCatalogFile(allFilesTable.getSelectedRow());
        Runnable downloadTask = new Runnable() {
            @Override
            public void run() {
                client.downloadFile(catalogDesc);
            }
        };
        (new Thread(downloadTask)).start();
    }//GEN-LAST:event_allDownloadBtnActionPerformed

    private void allUpdateBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_allUpdateBtnActionPerformed
        CatalogFile fileToUpdate = allFilesModel.getCatalogFile(allFilesTable.getSelectedRow());
        if (fileToUpdate != null) {
            JFileChooser browseDlg = new JFileChooser();
            int result = browseDlg.showOpenDialog((Component) this);
            if (result == JFileChooser.APPROVE_OPTION) {
                String filePath = browseDlg.getSelectedFile().getPath();
                File file = new File(filePath);
                if (file.exists()) {
                    updateFile(file, fileToUpdate);
                }
            }
        }
    }//GEN-LAST:event_allUpdateBtnActionPerformed

    private void allDeleteBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_allDeleteBtnActionPerformed
        final CatalogFile selectedFile = allFilesModel.getCatalogFile(allFilesTable.getSelectedRow());
        Runnable deleteTask = new Runnable() {
            @Override
            public void run() {
                client.deleteFile(selectedFile);
            }
        };
        (new Thread(deleteTask)).start();
    }//GEN-LAST:event_allDeleteBtnActionPerformed

    private void myRefreshBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_myRefreshBtnActionPerformed
        Runnable getFilesTask = new Runnable() {
            @Override
            public void run() {
                client.getMyFiles();
            }
        };
        (new Thread(getFilesTask)).start();
    }//GEN-LAST:event_myRefreshBtnActionPerformed

    private void unregisterMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_unregisterMenuItemActionPerformed
        Runnable unregisterTask = new Runnable() {
            @Override
            public void run() {
                client.unregister();
            }
        };
        (new Thread(unregisterTask)).start();
    }//GEN-LAST:event_unregisterMenuItemActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        if (!loginMenuItem.isEnabled()) {
            Runnable logoutTask = new Runnable() {
                @Override
                public void run() {
                    client.logout();
                }
            };
            (new Thread(logoutTask)).start();
        }

        dispose();
    }//GEN-LAST:event_formWindowClosing

    /**
     * @param args the command line arguments
     */
    public static void main(final String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CatalogClientUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CatalogClientUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CatalogClientUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CatalogClientUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new CatalogClientUI(args).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton allDeleteBtn;
    private javax.swing.JButton allDownloadBtn;
    private javax.swing.JTable allFilesTable;
    private javax.swing.JButton allRefreshBtn;
    private javax.swing.JButton allUpdateBtn;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JTabbedPane jTabbedPane2;
    private javax.swing.JMenuItem loginMenuItem;
    private javax.swing.JMenuItem logoutMenuItem;
    private javax.swing.JButton myDeleteBtn;
    private javax.swing.JButton myDownloadBtn;
    private javax.swing.JTable myFilesTable;
    private javax.swing.JButton myRefreshBtn;
    private javax.swing.JButton myUpdateBtn;
    private javax.swing.JButton myUploadBtn;
    private javax.swing.JMenuItem registerMenuItem;
    private javax.swing.JMenuItem unregisterMenuItem;
    // End of variables declaration//GEN-END:variables

    @Override
    public void updateAllFiles(final List<CatalogFile> files) {
        Runnable updateUI = new Runnable() {
            @Override
            public void run() {
                allFilesModel.setCatalogFileList(files);
            }
        };
        try {
            SwingUtilities.invokeAndWait(updateUI);
        } catch (InterruptedException ex) {
            Logger.getLogger(CatalogClientUI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(CatalogClientUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void updateMyFiles(final List<CatalogFile> files) {
        Runnable updateUI = new Runnable() {
            @Override
            public void run() {
                myFilesModel.setCatalogFileList(files);
            }
        };
        try {
            SwingUtilities.invokeAndWait(updateUI);
        } catch (InterruptedException ex) {
            Logger.getLogger(CatalogClientUI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(CatalogClientUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void deleteFile(final CatalogFile file) {
        Runnable updateUI = new Runnable() {
            @Override
            public void run() {
                allFilesModel.removeCatalogFile(file.getId());
            }
        };
        try {
            SwingUtilities.invokeAndWait(updateUI);
        } catch (InterruptedException ex) {
            Logger.getLogger(CatalogClientUI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(CatalogClientUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void uploadFile(final String fileName, final AccessPermission accessPerm, final WriteReadPermission writeReadPerm, final File file) {
        Runnable uploadFileTask = new Runnable() {
            @Override
            public void run() {
                BufferedInputStream input = null;
                try {
                    byte fileBuffer[] = new byte[(int) file.length()];
                    input = new BufferedInputStream(new FileInputStream(file.getPath()));
                    input.read(fileBuffer, 0, fileBuffer.length);
                    input.close();
                    if (accessPerm == AccessPermission.PRIVATE) {
                        client.uploadFile(fileName, accessPerm, null, fileBuffer);
                    } else if (accessPerm == AccessPermission.PUBLIC) {
                        client.uploadFile(fileName, accessPerm, writeReadPerm, fileBuffer);
                    }
                } catch (IOException ex) {
                    Logger.getLogger(CatalogClientUI.class.getName()).log(Level.SEVERE, null, ex);
                } finally {
                    try {
                        input.close();
                    } catch (IOException ex) {
                        Logger.getLogger(CatalogClientUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        };

        (new Thread(uploadFileTask)).start();
    }

    @Override
    public void deleteMyFile(final CatalogFile file) {
        Runnable updateUI = new Runnable() {
            @Override
            public void run() {
                myFilesModel.removeCatalogFile(file.getId());
            }
        };
        try {
            SwingUtilities.invokeAndWait(updateUI);
        } catch (InterruptedException ex) {
            Logger.getLogger(CatalogClientUI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(CatalogClientUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void login(final String name, final String pwd) {
        Runnable loginTask = new Runnable() {
            @Override
            public void run() {
                client.login(name, pwd);
            }
        };

        (new Thread(loginTask)).start();
    }

    @Override
    public void saveFile(byte[] file) {
        JFileChooser saveFileDlg = new JFileChooser();
        int retrieval = saveFileDlg.showSaveDialog((Component) this);

        if (retrieval == JFileChooser.APPROVE_OPTION) {
            OutputStream out = null;
            try {
                String filePath = saveFileDlg.getSelectedFile().getPath();
                InputStream in = new ByteArrayInputStream(file);
                out = new FileOutputStream(new File(filePath));
                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                in.close();
                out.close();
            } catch (IOException ex) {
                Logger.getLogger(CatalogClientUI.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    out.close();
                } catch (IOException ex) {
                    Logger.getLogger(CatalogClientUI.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    @Override
    public void setUserName(final String name) {
        Runnable updateUI = new Runnable() {
            @Override
            public void run() {
                setTitle("File Catalog --- USER: " + name);
            }
        };
        try {
            SwingUtilities.invokeAndWait(updateUI);
        } catch (InterruptedException ex) {
            Logger.getLogger(CatalogClientUI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(CatalogClientUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void register(final String name, final String pwd) {
        Runnable loginTask = new Runnable() {
            @Override
            public void run() {
                client.register(name, pwd);
            }
        };

        (new Thread(loginTask)).start();
    }

    @Override
    public void updateAfterLogin(final String userName) {
        Runnable updateUI = new Runnable() {
            @Override
            public void run() {
                setTitle("File Catalog --- USER: " + userName);
                unregisterMenuItem.setEnabled(true);
                logoutMenuItem.setEnabled(true);
                loginMenuItem.setEnabled(false);
                registerMenuItem.setEnabled(false);
            }
        };
        try {
            SwingUtilities.invokeAndWait(updateUI);
        } catch (InterruptedException ex) {
            Logger.getLogger(CatalogClientUI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(CatalogClientUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void updateFile(final File file, final CatalogFile fileDescription) {
        Runnable uploadFileTask = new Runnable() {
            @Override
            public void run() {
                BufferedInputStream input = null;
                try {
                    byte fileBuffer[] = new byte[(int) file.length()];
                    input = new BufferedInputStream(new FileInputStream(file.getPath()));
                    input.read(fileBuffer, 0, fileBuffer.length);
                    input.close();
                    client.updateFile(fileDescription, fileBuffer);
                } catch (IOException ex) {
                    Logger.getLogger(CatalogClientUI.class.getName()).log(Level.SEVERE, null, ex);
                } finally {
                    try {
                        input.close();
                    } catch (IOException ex) {
                        Logger.getLogger(CatalogClientUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        };

        (new Thread(uploadFileTask)).start();
    }

    @Override
    public void updateAfterLogout() {
        Runnable updateUI = new Runnable() {
            @Override
            public void run() {
                setTitle("File Catalog");
                unregisterMenuItem.setEnabled(false);
                logoutMenuItem.setEnabled(false);
                loginMenuItem.setEnabled(true);
                registerMenuItem.setEnabled(true);
            }
        };
        try {
            SwingUtilities.invokeAndWait(updateUI);
        } catch (InterruptedException ex) {
            Logger.getLogger(CatalogClientUI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(CatalogClientUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void showRemoteExceptionNotification(final String msg) {
        final java.awt.Component c = this;
        Runnable updateUI = new Runnable() {
            @Override
            public void run() {
                JOptionPane.showMessageDialog(c, msg, "Network Problem", JOptionPane.ERROR_MESSAGE);
            }
        };
        try {
            SwingUtilities.invokeAndWait(updateUI);
        } catch (InterruptedException ex) {
            Logger.getLogger(CatalogClientUI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(CatalogClientUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void showRejectedExceptionNotification(final String msg) {
        final java.awt.Component c = this;
        Runnable updateUI = new Runnable() {
            @Override
            public void run() {
                JOptionPane.showMessageDialog(c, msg, "Rejected Exception", JOptionPane.ERROR_MESSAGE);
            }
        };
        try {
            SwingUtilities.invokeAndWait(updateUI);
        } catch (InterruptedException ex) {
            Logger.getLogger(CatalogClientUI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(CatalogClientUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
